package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultVo;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.CityMapper;
import com.ant.storage.pojo.City;
import com.ant.storage.service.ICityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityServiceImpl extends ServiceImpl<CityMapper,City> implements ICityService{

    @Override
    public Integer queryCityId(String city) {
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",city);
        City city1 = this.getOne(queryWrapper);
        if(city1.getId() != null){
            return city1.getId();
        }
        return null;
    }

    @Override
    public String idQuery(Integer city_id)  {
        QueryWrapper<City>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",city_id);
        City city = this.getOne(queryWrapper);
        if(city == null){
            throw new AntException(ResultCode.DEPT_ERROR);
        }
        return city.getName();
    }

    @Override
    public ResultVo queryCity() {
        QueryWrapper<City>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",0);
        List<City>list = this.list(queryWrapper);
        return ResultVo.success("ok",list);
    }

    @Override
    public ResultVo queryDept(Integer city_id) {
        if(city_id != null){
            QueryWrapper<City>queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",city_id);
            List<City>list = this.list(queryWrapper);
            return ResultVo.success("ok",list);
        }else{
            QueryWrapper<City>queryWrapper = new QueryWrapper<>();
            queryWrapper.ne("pid",0);
            List<City>list = this.list(queryWrapper);
            return ResultVo.success("ok",list);
        }
    }
}
